<%@ Page Language="C#" MasterPageFile="~/master/admin.master" AutoEventWireup="true"
    CodeFile="customer.aspx.cs" Inherits="admin_customer" Title="Untitled Page" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Register Namespace="ECommerce.Library.Web" TagPrefix="ucc" %>
<asp:Content ID="cntMiddleContent" ContentPlaceHolderID="cphMiddleContent" runat="Server">
    <script type="text/javascript">

        var CustomerName_Render = function (value, p, record, rowIndex) {
            var categoryUrl = "<%= SiteGlobals.BaseUrl %>" + "admin/customer-edit.aspx?cuid=" + record.data.UserId;
            return '<a href="' + categoryUrl + '" class="leftlink">' + String.format("{0}, {1}", record.data.LastName, record.data.FirstName) + '</a>';
        };

        var Address_Renderer = function (value, p, record, rowIndex) {
            var address1 = record.data.Address1;
            var address2 = record.data.Address2;
            return String.format("{0}<br />{1}", address1, address2);
        };
        var State_Renderer = function (value, p, record, rowIndex) {
            var state = record.data.State;
            return String.format("{0}", state);
        };
        var Zip_Renderer = function (value, p, record, rowIndex) {
            var zip = record.data.ZipCode;
            return String.format("{0}", zip);
        };

        var IsActive_Renderer = function (value) {
            if (value == true)
                return "Yes";
            else
                return "No";
        }

        var CommandHandler = function (command, rowIndex, grid) {
            //showObj(grid);
            grid.getRowEditor().stopEditing();
            grid.getView().refresh();
            grid.getSelectionModel().selectRow(rowIndex);
            grid.getRowEditor().startEditing(rowIndex);
        }

        var afterEdit = function (e) {

            e.grid.stopEditing();

            // Call DirectMethod
            Polana.WebService.UpdateCustomer(e.record.data.UserId, e.record.data.EmailAddress);
        };

        var categoryGrid;

        var deleteCustomers = function () {
            if (categoryGrid != null) {
                categoryGrid.getRowEditor().stopEditing();

                var s = categoryGrid.getSelectionModel().getSelections();
                if (s.length > 0) {

                    if (confirm("Are you sure you want to delete these customers?")) {

                        var ids = "";

                        for (var i = 0, r; r = s[i]; i++) {
                            ids = ids + " " + r.data.UserId;
                        }

                        Polana.WebService.DeleteCustomers(ids);
                    }
                }
            }
        }

        var searchCustomer = function (grid) {
            Polana.WebService.SearchCustomer();
        }

      

        var onDocumentReady = function (grid) {
            categoryGrid = grid;
        }

        var addCustomer = function () {
            window.location = "<%= SiteGlobals.BaseUrl %>" + "admin/customer-add.aspx";
        }
    </script>
    <ext:ResourceManager ID="ScriptManager1" runat="server" Theme="Gray">
        <Listeners>
            <DocumentReady Fn="onDocumentReady(#{GridPanel1})" />
        </Listeners>
    </ext:ResourceManager>
    <ext:Store ID="Store1" runat="server" RemoteSort="true" OnRefreshData="Store1_RefreshData">
        <AutoLoadParams>
            <ext:Parameter Name="sort" Value="UserName" Mode="Value" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="limit" Value="50" Mode="Raw" />
        </AutoLoadParams>
        <Proxy>
            <ext:PageProxy />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="UserId">
                <Fields>
                    <ext:RecordField Name="UserId" />
                    <ext:RecordField Name="UserName" />
                    <ext:RecordField Name="FirstName" ServerMapping="Profile.FirstName" />
                    <ext:RecordField Name="LastName" ServerMapping="Profile.LastName" />
                    <ext:RecordField Name="EmailAddress" ServerMapping="Profile.EmailAddress" />
                    <ext:RecordField Name="Address1" ServerMapping="Profile.BillingAddress.AddressLine1" />
                    <ext:RecordField Name="Address2" ServerMapping="Profile.BillingAddress.AddressLine2" />
                    <ext:RecordField Name="City" ServerMapping="Profile.BillingAddress.City" />
                    <ext:RecordField Name="State" ServerMapping="Profile.BillingAddress.AddressState" />
                    <ext:RecordField Name="ZipCode" ServerMapping="Profile.BillingAddress.ZipCode" />
                    <ext:RecordField Name="IsActive" Type="Boolean" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <div class="admin-function-top inline">
        <ucc:ResponseMessage ID="uccResponseMessage" runat="server"></ucc:ResponseMessage>
        <ext:GridPanel ID="GridPanel1" runat="server" EnableViewState="true" AutoHeight="true"
            StoreID="Store1">
            <ColumnModel>
                <Columns>
                    <ext:Column Header="Name" Width="80" DataIndex="FirstName">
                        <Renderer Fn="CustomerName_Render" />
                        <%--<Editor>
                            <ext:MultiField DataIndex="FirstName">
                                <Fields>
                                    <ext:TextField ID="txtLastName" runat="server" DataIndex="LastName" />
                                    <ext:TextField ID="txtFirstName" runat="server" DataIndex="FirstName" />
                                </Fields>
                            </ext:MultiField>
                        </Editor>--%>
                    </ext:Column>
                    <ext:Column Header="Email" Width="60" DataIndex="EmailAddress">
                        <Editor>
                            <ext:TextField ID="txtEmail" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Address" Width="50" DataIndex="Address1" Sortable="false">
                        <Renderer Fn="Address_Renderer" />
                    </ext:Column>
                    <ext:Column Header="State" Width="20" DataIndex="State" Sortable="false">
                        <Renderer Fn="State_Renderer" />
                    </ext:Column>
                    <ext:Column Header="Zip" Width="20" DataIndex="Zip" Sortable="false">
                        <Renderer Fn="Zip_Renderer" />
                    </ext:Column>
                    <ext:Column Header="Active" DataIndex="IsActive" Width="0" Sortable="false" Hidden="true">
                        <Renderer Fn="IsActive_Renderer" />
                        <Editor>
                            <ext:Checkbox ID="chkIsActive" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:CommandColumn Width="30">
                        <Commands>
                            <ext:GridCommand Icon="NoteEdit" CommandName="Edit" Text="Edit" />
                        </Commands>
                    </ext:CommandColumn>
                </Columns>
            </ColumnModel>
            <TopBar>
                <ext:Toolbar ID="ActionToolBar" runat="server">
                    <Items>
                        <ext:TextField ID="txtToolbarSearch" runat="server" EmptyText="Search by Customer Name"
                            FieldClass="text" EnableKeyEvents="true">
                            <Listeners>
                                <KeyPress Handler="if(e.getKey() == Ext.EventObject.ENTER){e.preventDefault(); searchCustomer(#{GridPanel1})}" />
                            </Listeners>
                        </ext:TextField>
                        <ext:Button ID="btnToolbarSearch" runat="server" Text="Search" Icon="Magnifier">
                            <Listeners>
                                <Click Handler="searchCustomer(#{GridPanel1});" />
                            </Listeners>
                        </ext:Button>
                        <ext:Button ID="btnToolbarAddCategory" runat="server" Text="Add Customer" Icon="Add">
                            <Listeners>
                                <Click Handler="addCustomer();" />
                            </Listeners>
                        </ext:Button>
                        <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                        <ext:Button ID="btnToolbarDelete" runat="server" Text="Delete Selected" Icon="Delete">
                            <Listeners>
                                <Click Handler="deleteCustomers();" />
                            </Listeners>
                        </ext:Button>
                    </Items>
                </ext:Toolbar>
            </TopBar>
            <BottomBar>
                <ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="50" DisplayInfo="true"
                    DisplayMsg="Displaying customers {0} - {1} of {2}" EmptyMsg="No customers to display"
                    AutoWidth="true" />
            </BottomBar>
            <LoadMask ShowMask="true" />
            <SelectionModel>
                <ext:CheckboxSelectionModel ID="CheckboxSelectionModel1" runat="server" ColumnPosition="7"
                    AllowDeselect="true" CheckOnly="true" />
            </SelectionModel>
            <Plugins>
                <ext:RowEditor ID="rowedi" runat="server" SaveText="Update" ClicksToEdit="2" ErrorSummary="false"
                    ShowTooltip="={Ext.emptyFn}">
                    <Listeners>
                        <AfterEdit Fn="afterEdit" />
                    </Listeners>
                </ext:RowEditor>
            </Plugins>
            <Listeners>
                <Command Handler="CommandHandler(command, rowIndex, this)" />
            </Listeners>
            <View>
                <ext:GridView ID="GridView1" runat="server" ForceFit="true" />
            </View>
        </ext:GridPanel>
    </div>
</asp:Content>
